﻿using System;
using Soundteller.AudioVisual.Repositories.Interfaces;
using Soundteller.AudioVisual.Services.Interfaces;
using Soundteller.Audio.Model;
using Soundteller.Bus.Model.Interfaces;

namespace Soundteller.AudioVisual.Services
{
    public class PadStateUpdater : IPadStateUpdater
    {
        private readonly IPadRepository _padRepository;

        public PadStateUpdater(IPadRepository padRepository)
        {
            if (padRepository == null) throw new ArgumentNullException("padRepository");

            _padRepository = padRepository;
        }

        public void SetInPlayingState(string soundName)
        {
            if (soundName == null) throw new ArgumentNullException("soundName");

            var pad = _padRepository.GetBySoundName(soundName);
            if (pad == null)
                throw new InvalidOperationException("Cannot set pad state, sound not found");

            pad.Graphic.Name = "pad_playing";
        }

        public void Handle(IMessage message)
        {
            if (message == null) throw new ArgumentNullException("message");

            var soundBufferedMessage = message as SoundPlayedMessage;
            if (soundBufferedMessage == null)
                throw new ArgumentException("Message should have type SoundBufferedMessage", "message");


            SetInPlayingState(soundBufferedMessage.SoundName);
        }

        public Type GetMessageType()
        {
            return typeof(SoundPlayedMessage);
        }
    }
}